<?php

class chuckView {

	public function randomChuck() {

		try {

			error_reporting(E_ALL);

			$request = 'http://api.icndb.com/jokes/random';

			// Make the request
			$json = file_get_contents($request);

			// Retrieve HTTP status code
			list($version, $status_code, $msg) = explode(' ', $http_response_header[0], 3);

			// Check the HTTP Status code
			switch($status_code) {
				case 200 :
				// Success
				//echo"LYCKAT" . ": STATUS " .$status_code. "</br>";
					break;
				case 503 :
					die('503 means: Service unavailable. An internal problem prevented us from returning data to you.');
					break;
				case 403 :
					die('403 means: Forbidden. You do not have permission to access this resource, or are over your rate limit.');
					break;
				case 404 :
					die('404 means: Ooops! Sidan hittades ej');
					break;
				case 400 :
				// You may want to fall through here and read the specific XML error
					die('400 means:  Bad request. The parameters passed to the service did not match as expected.');
					break;
				default :
					die('returned an unexpected HTTP status of:' . $status_code);
			}

			$json = json_decode($json);
			//dekodar av...

			echo($json -> value -> joke);
			//och spottar ut
			//echo ($json->value->id); //Vilket nummer skämtet har
		} catch (exception $e) {
			echo "Apiet Funkar ej för tillfället...";
		}

	}

}
